package com.activity.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.activity.App;
import com.activity.PersonInfoActivity;
import com.activity.bean.OpusBean;
import com.activity.bean.UserBean;
import com.baoyz.actionsheet.ActionSheet;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.activity.widget.BLinearLayout;
import com.example.apple.ddhr_new.utils.ActivityUtil;
import com.example.apple.ddhr_new.utils.ImageViewUtil;
import com.example.apple.ddhr_new.utils.ScreenUtil;
import com.example.lzbase.BaseActivity;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Person1Adapter extends GroupedRecyclerViewAdapter {


    public ArrayList<OpusBean> data = new ArrayList();



    public Person1Adapter(Context context) {
        super(context);

    }


    @Override
    public boolean hasHeader(int groupPosition) {

        return true;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return false;
    }

    @Override
    public int getGroupCount() {

        return 1;
    }


    @Override
    public int getChildrenCount(int groupPosition) {

        return data.size() ;

    }

    @Override
    public int getChildViewType(int groupPosition, int childPosition) {

        return TYPE_CHILD;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.header_home;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return R.layout.item_empty;
    }

    @Override
    public int getChildLayout(int viewType) {



        return R.layout.item_personal_cell;
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {


//        621/467


        ImageView imageView= holder.itemView.findViewById(R.id.iv_icon);
      LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
        WindowManager m = ((Activity) mContext).getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
      params.height = (int) (d.getWidth()*(467.0/621.0));
      imageView.setLayoutParams(params);

    }

    @Override
    public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {

    }
    public PersonAdapter.personListener personListener;

    @Override
    public void onBindChildViewHolder(BaseViewHolder holder, int groupPosition, int childPosition) {


            ((PersonalHolder) holder).setData(data.get(childPosition));

        ((PersonalHolder) holder).iv_praise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(personListener!=null){
                    personListener.praiseClick(childPosition);
                }

            }
        });



    }

    //
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_CHILD) {




            View view = LayoutInflater.from(mContext).inflate(getChildLayout(viewType), parent, false);

            return new PersonalHolder(view);

        }
        return super.onCreateViewHolder(parent, viewType);
    }


    public class PersonalHolder extends BaseViewHolder {

        @BindView(R.id.iv_icon)
        ImageView ivIcon;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.iv_user_icon)
        ImageView ivUserIcon;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.iv_dianzan)
        ImageView iv_praise;
        public PersonalHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(OpusBean data) {

            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ivIcon.getLayoutParams();


            WindowManager m = ((Activity) mContext).getWindowManager();
            Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
            params.width = (d.getWidth()-ScreenUtil.dp2px(mContext,12))/2;


            if(data.getWorkssizes()!=null){
                ArrayList<String> sizes = new ArrayList<String>(Arrays.asList(StringUtils.split( data.getWorkssizes(),"|")));

                if(sizes.size()>0){
                    ArrayList<String> size1 =new ArrayList<String>(Arrays.asList(StringUtils.split(sizes.get(0),",")));
                    params.height =(int)(params.width*Float.valueOf(size1.get(1))/Float.valueOf(size1.get(0)));
                    ivIcon.setLayoutParams(params);

                }else
                {
                    params.height =params.width;
                    ivIcon.setLayoutParams(params);
                }
            }else {
                params.height =params.width;
                ivIcon.setLayoutParams(params);
            }


            if (data.images().size() > 0) {

                RoundedCorners roundedCorners= new RoundedCorners(10);
                RequestOptions options=RequestOptions.bitmapTransform(roundedCorners).override(params.width, params.height);
                options.placeholder(R.drawable.default_work);
                Glide.with(mContext).load(data.images().get(0)).apply(options).into(ivIcon);

            } else {
                ivIcon.setImageResource(R.drawable.meinv);
            }

            tvName.setText(data.getUsernickname());
            tvContent.setText(data.getWorkstitle());
            tvNum.setText(data.getWorkspraisenum());

            if(data.getWorkifpraise()!=null&&data.getWorkifpraise().equals("1")) {
                iv_praise.setImageResource(R.drawable.dianzan1);
            }else {
                iv_praise.setImageResource(R.drawable.dianzan);
            }
            ImageViewUtil.loadCircleImage(data.getUsericon(), ivUserIcon, mContext);


        }


    }


}
